// pages/rateList/rateList.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ratecount:0,
    rateList:[],
    tablist: ['全部', '有图'],
    goodsid:0,
    current:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id=options.id;
    this.setData({ goodsid:id})
    let self=this;
    wx.request({
      url: app.globalData.reqPath + 'shop/GoodsComments',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        productId: id
      },
      success: function ({ data }) {
        if (data.code == 200) {
          let list = data.data;
          self.setData({ rateList: data.data.list, ratecount: data.data.dataCount });
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeList:function(e){
    let index = e.currentTarget.dataset.index;
    this.setData({ current: index });
    let id = this.data.goodsid;
    let data = null;
    if (index == 0) {
      data = {
        productId: id,
        limit: -1
      }
    } else if (index == 1) {
      data = {
        productId: id,
        limit: -1,
        hasImg: 1
      }
    } 
    this.getrateList(data)
  },
  getrateList: function (data) {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/GoodsComments',
      header: {
        'authorization': app.globalData.authorization
      },
      data: data,
      success: function ({ data, header }) {
        if (data.code == 200) {
          if (header.authorization && header.authorization != app.globalData.authorization) {
            app.globalData.authorization = header.authorization;
          }
          let list = data.data.list;
          let len = data.data.dataCount;
          list = list.map(function (item) {
            // str.substr(0, 2) + '***' + str.substr(5, str.split('').length);
           // item.nickname = item.nickname[0] + '***' + item.nickname[item.nickname.length - 1];
            return item;
          })
          self.setData({ rateList: list })
        }
      }
    })
  },
  previewImg:function(e){//图片预览
    let list = e.currentTarget.dataset.list;
    let current = e.currentTarget.dataset.current;
    wx.previewImage({
      current:current,
      urls: list
    })


  }
})